CC      = arm-linux-gcc
LD      = arm-linux-ld
AR      = arm-linux-ar
OBJCOPY = arm-linux-objcopy
OBJDUMP = arm-linux-objdump

NAME        := app
ELF         := $(NAME).elf
BIN         := $(NAME).bin
ASM         := $(NAME).dis
LDS         := $(NAME).lds
INCLUDEDIR  := $(shell pwd)/include
CFLAGS      := -Wall -O2
CPPFLAGS    := -nostdinc -I$(INCLUDEDIR)

export 	CC LD AR OBJCOPY OBJDUMP INCLUDEDIR CFLAGS CPPFLAGS

OBJS := head.o init.o nand.o interrupt.o serial.o lcddrv.o framebuffer.o lcdlib.o main.o 
OBJS += led.o
OBJS += key.o
OBJS += timer.o
OBJS += lib/libc.a

all: clean $(BIN) 

$(BIN): $(OBJS)
	${LD} -T$(LDS) -o $(ELF) $^
	${OBJCOPY} -O binary -S $(ELF) $@
	${OBJDUMP} -D -m arm $(ELF) > $(ASM)

.PHONY : lib/libc.a
lib/libc.a:
	cd lib; make; cd ..
	
%.o:%.c
	${CC} $(CPPFLAGS) $(CFLAGS) -c -o $@ $<

%.o:%.S
	${CC} $(CPPFLAGS) $(CFLAGS) -c -o $@ $<

clean:
	make  clean -C lib
	rm -f $(BIN) $(ELF) $(ASM) *.o

.PHONY: clean all
	
